package me.greyliu.kuai.dao;
import me.greyliu.kuai.App;
import me.greyliu.kuai.entity.User;

import java.sql.*;

public class UserDao {
    public User getUserByName(String name){
        try {
            Statement stmt = App.c.createStatement();
            ResultSet rs = stmt.executeQuery( "SELECT * FROM USER WHERE NAME='"+name+"';" );
            rs.next();
            String  pwd = rs.getString("pwd");
            rs.close();
            stmt.close();
            if(pwd!=null)
                return new User(name,pwd);
        }catch( Exception e ) {
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
        }
        return null;
    }
    public void InsertUser(String name,String pwd){
        try {
            Statement stmt = App.c.createStatement();
            stmt.executeUpdate("INSERT INTO USER (NAME,PWD) " +
                    "VALUES ('"+name+"','"+pwd+"');");
            stmt.close();
            App.c.commit();
        }catch( Exception e ) {
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
        }
    }
}
